package com.renwei.yunlong.activity.me;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.renwei.yunlong.R;
import com.renwei.yunlong.activity.contacts.CompanyCertificateActivity;
import com.renwei.yunlong.base.BaseActivity;
import com.renwei.yunlong.bean.FriendEnterpriseBean;
import com.renwei.yunlong.http.HttpTaskListener;
import com.renwei.yunlong.utils.LogUtil;
import com.renwei.yunlong.utils.ServiceRequestManager;
import com.renwei.yunlong.utils.StringUtils;
import com.renwei.yunlong.utils.CommonUtils;
import com.zyf.fwms.commonlibrary.utils.StatusBarUtil;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Project :  yunaandroid.
 * Package name: com.renwei.yunlong.activity
 * Created by :  Benjamin.
 * Created time: 2017/10/30 18:35
 * Changed by :  Benjamin.
 * Changed time: 2017/10/30 18:35
 * Class description:
 */

public class DisplayEnterpriseInfoActivity extends BaseActivity implements View.OnClickListener, HttpTaskListener {


    @Bind(R.id.iv_back)
    ImageView ivBack;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.tv_right)
    TextView tvRight;
    @Bind(R.id.tv_company_name)
    TextView tvCompanyName;
    @Bind(R.id.tv_numbering)
    TextView tvNumbering;
    @Bind(R.id.tv_type)
    TextView tvType;
    @Bind(R.id.text_certif_status)
    TextView textCertifStatus;
    @Bind(R.id.tv_certif_status)
    TextView tvCertifStatus;
    @Bind(R.id.rl_certification)
    RelativeLayout rlCertification;
    @Bind(R.id.text_ode_next)
    TextView textOdeNext;
    @Bind(R.id.iv_code_next)
    ImageView ivCodeNext;
    @Bind(R.id.rl_code)
    RelativeLayout rlCode;
    @Bind(R.id.text_contact)
    TextView textContact;
    @Bind(R.id.tv_contact)
    TextView tvContact;
    @Bind(R.id.rl_contact)
    RelativeLayout rlContact;
    @Bind(R.id.text_phone)
    TextView textPhone;
    @Bind(R.id.tv_phone)
    TextView tvPhone;
    @Bind(R.id.rl_phone)
    RelativeLayout rlPhone;
    @Bind(R.id.text_documents)
    TextView textDocuments;
    @Bind(R.id.tv_documents)
    TextView tvDocuments;
    @Bind(R.id.rl_documents)
    RelativeLayout rlDocuments;
    @Bind(R.id.text_certificate_number)
    TextView textCertificateNumber;
    @Bind(R.id.tv_certificate_number)
    TextView tvCertificateNumber;
    @Bind(R.id.rl_business)
    RelativeLayout rlBusiness;
    @Bind(R.id.text_alive_years)
    TextView textAliveYears;
    @Bind(R.id.tv_alive_years)
    TextView tvAliveYears;
    @Bind(R.id.rl_alive)
    RelativeLayout rlAlive;
    @Bind(R.id.text_photo_next)
    TextView textPhotoNext;
    @Bind(R.id.iv_photo_next)
    ImageView ivPhotoNext;
    @Bind(R.id.tv_photo_next)
    TextView tvPhotoNext;
    @Bind(R.id.rl_photo)
    RelativeLayout rlPhoto;
    @Bind(R.id.text_address)
    TextView textAddress;
    @Bind(R.id.tv_address)
    TextView tvAddress;
    @Bind(R.id.rl_address)
    RelativeLayout rlAddress;
    @Bind(R.id.text_qualification)
    TextView textQualification;
    @Bind(R.id.iv_qualification)
    ImageView ivQualification;
    @Bind(R.id.tv_certificate_count)
    TextView tvCertificateCount;
    @Bind(R.id.rl_qualification)
    RelativeLayout rlQualification;
    FriendEnterpriseBean enterpriseBean;
    private String companyCode;


    public static void OpenActivity(Context context, String companyCode) {
        Intent intent = new Intent(context, DisplayEnterpriseInfoActivity.class);
        intent.putExtra("companyCode", companyCode);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_enterprise_info_display);
        StatusBarUtil.setColor(this, CommonUtils.getColor(this, R.color.title_color), 0);
        ButterKnife.bind(this);

        initView();
    }


    //初始化数据
    public void initView() {

//        if (!TextUtils.isEmpty(isSelfFlag) && "1".equals(isSelfFlag)) { //自建企业

        ivBack.setVisibility(View.VISIBLE);
        /**判断权限、管理员权限可以编辑、其他不可编辑**/
        tvRight.setVisibility(View.GONE);
        tvTitle.setText("企业信息");
        ivBack.setOnClickListener(this);
        tvRight.setOnClickListener(this);
        ivBack.setOnClickListener(this);
        tvRight.setOnClickListener(this);
        rlQualification.setOnClickListener(this);
        rlPhoto.setOnClickListener(this);
        rlCertification.setOnClickListener(this);
        rlCode.setOnClickListener(this);

        initData();
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                this.finish();
                break;
            case R.id.rl_qualification:
                Intent certificateIntent = new Intent(this, CompanyCertificateActivity.class);
                certificateIntent.putExtra("from", "friend");
                certificateIntent.putExtra("certificateType", "32");
                certificateIntent.putExtra("friendCompanyCode", enterpriseBean == null ? "" : enterpriseBean.getRows().getCompanyCode());
                certificateIntent.putExtra("title", "资质证书");
                startActivity(certificateIntent);
                return;
            case R.id.rl_photo:
                Intent photoIntent = new Intent(this, CompanyCertificateActivity.class);
                photoIntent.putExtra("from", "friend");
                photoIntent.putExtra("certificateType", "31");
                photoIntent.putExtra("title", "证件");
                photoIntent.putExtra("friendCompanyCode", enterpriseBean == null ? "" : enterpriseBean.getRows().getCompanyCode());
                startActivity(photoIntent);
                break;
            case R.id.rl_code:
                QRCodeActivity.openActivity(this, companyCode);
                break;
            default:
                break;
        }
    }

    //初始化数据
    public void initData() {
        companyCode = getIntent().getStringExtra("companyCode") == null ? "" : getIntent().getStringExtra("companyCode");
        ServiceRequestManager.getManager().QueryFirendCompany(this, companyCode, this);
    }


    @Override
    public void onSuccess(int requestId, String result) {
        LogUtil.e("好友企业信息展示页面  ：" + result);
        switch (requestId) {
            case 879:
                enterpriseBean = new Gson().fromJson(result, FriendEnterpriseBean.class);
                if (enterpriseBean.getMessage().getCode() == 200) {
                    if (enterpriseBean.getRows() != null) {
                        FriendEnterpriseBean.RowsBean company = enterpriseBean.getRows();
                        tvCompanyName.setText(company.getCompanyName());
                        tvNumbering.setText(company.getCompanyCode());
                        if ("1".equals(enterpriseBean.getRows().getType())) {
                            tvType.setText("/" + "业主");
                        } else {
                            tvType.setText("/" + "服务端");
                        }

                        //认证状态 0：未认证，1：待认证，2已认证，3未通过认证
                        if ("0".equals(company.getCheckFlag())) {
                            tvCertifStatus.setText("未认证");
                        } else if ("1".equals(company.getCheckFlag())) {
                            tvCertifStatus.setText("待认证");
                        } else if ("2".equals(company.getCheckFlag())) {
                            tvCertifStatus.setText("已认证");
                        } else if ("3".equals(company.getCheckFlag())) {
                            tvCertifStatus.setText("未通过认证");
                        }
                        tvContact.setText(company.getRegisterName());//联系人
                        tvPhone.setText(company.getCustomerPhone());//电话
                        if ("1".equals(company.getCertificateType())) {
                            tvDocuments.setText("营业执照");
                        } else if ("2".equals(company.getCertificateType())) {
                            tvDocuments.setText("组织机构代码证");
                        }
                        tvCertificateNumber.setText(company.getCertificateNumber());//证件号码
                        tvAliveYears.setText(company.getEffectiveAge());
                        tvAddress.setText(StringUtils.value(company.getProvinceId()) + StringUtils.value(company.getCityId()) + StringUtils.value(company.getRegionId()) + StringUtils.value(company.getRegisterAddr()));
                        tvCertificateCount.setText("查看");
                    }
                } else {
                    showCenterInfoMsg("网络错误");
                }
                break;
            default:
                break;
        }
    }

    @Override
    public void onException(int requestId, String code) {
        showTopWrongMsg(getResources().getString(R.string.net_work_error));
    }


}
